package com.itheima.job;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Calendar;
import java.util.Date;

public class CleanAndSaveOrderSetting {
   @Reference
  OrderSettingService orderSettingService;


    public void clearLastMonthData()  {
    try {
        //获取当前时间
        Date date = new Date();
        //获取上月当天时间
        String lastMonthDate = getLastMonthDate(date);
       orderSettingService.clearLastMonthData(lastMonthDate);



    } catch (Exception e) {
        e.printStackTrace();
    }


}
   //获取上个月最后一天
    private String getLastMonthDate(Date date) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        date = calendar.getTime();
        String lastMonth = DateUtils.parseDate2String(date);
        int i = lastMonth.lastIndexOf("-");
        String lastYearAndMonth = lastMonth.substring(0, i);
        String lastMonthDay = lastYearAndMonth+"-31";

          return lastMonthDay;
    }


}
